package com.voxel.sense.infomsg.module.user.staff;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.utils.PhoneUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.utils.GlideUtils;

import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.model.api.response.StaffEntity;

import butterknife.BindView;

/**
 * Created by ChenYiYao on 2018/7/27.
 */
public class StaffAdapter extends RecyclerAdapter<StaffEntity.SysUserListBean> {

    public StaffAdapter() {
    }

    public StaffAdapter(AdapterListener<StaffEntity.SysUserListBean> listener) {
        super(listener);
    }

    @Override
    protected int getItemViewType(int position, StaffEntity.SysUserListBean staffEntity) {
        return R.layout.item_recycle_staff;
    }

    @Override
    protected RecyclerAdapter.ViewHolder<StaffEntity.SysUserListBean> onCreateViewHolder(View root, int viewType) {
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder<StaffEntity.SysUserListBean> holder, int position) {
        super.onBindViewHolder(holder, position);
    }

    static class ViewHolder extends RecyclerAdapter.ViewHolder<StaffEntity.SysUserListBean> {
        @BindView(R.id.iv_pic)
        ImageView iv_pic; //头像
        @BindView(R.id.tv_name)
        TextView tv_name;  //名字
        @BindView(R.id.tv_department)
        TextView tv_department; //部门
        @BindView(R.id.tv_user_type)
        TextView tv_user_type; //用户类型
        @BindView(R.id.tv_phone)
        TextView tv_phone; //电话
        @BindView(R.id.tv_email)
        TextView tv_email; //邮箱
        @BindView(R.id.tv_status)
        TextView tv_status; //审核标记
        @BindView(R.id.iv_phone)
        ImageView iv_phone;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(StaffEntity.SysUserListBean staffEntity) {
            String realname = staffEntity.getRealname();
            String username = staffEntity.getUsername();
            tv_name.setText(TextUtils.isEmpty(realname) ? username : (username + "(" + realname + ")"));

            String email = staffEntity.getEmail();
            if (TextUtils.isEmpty(email)) {

                tv_email.setVisibility(View.INVISIBLE);
            } else {
                tv_email.setVisibility(View.VISIBLE);
                tv_email.setText(staffEntity.getEmail());
            }

            String userType = staffEntity.getUserType();

            if (TextUtils.isEmpty(userType)) {
                tv_department.setVisibility(View.INVISIBLE);
            } else {
                tv_department.setVisibility(View.VISIBLE);
                tv_department.setText(staffEntity.getUserType());
            }

            if (Constants.INFOMARL_USER_YPE.equals(staffEntity.getIspass())) {
                tv_status.setVisibility(View.VISIBLE);
            } else {
                tv_status.setVisibility(View.GONE);

            }


            final String phone = staffEntity.getPhone();
            if (TextUtils.isEmpty(phone)) {
                iv_phone.setVisibility(View.GONE);
                tv_phone.setVisibility(View.GONE);
            } else {
                iv_phone.setVisibility(View.VISIBLE);
                tv_phone.setVisibility(View.VISIBLE);
                tv_phone.setText(phone);
                iv_phone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PhoneUtils.dial(phone);
                    }
                });
            }


            StaffEntity.SysUserListBean.SysUserDetailsBean sysUserDetails = staffEntity.getSysUserDetails();
            if (sysUserDetails != null) {
                GlideUtils.GlideLoadPortrait(tv_user_type.getContext(), sysUserDetails.getHeadPhotoAddr(), iv_pic);
                if (!TextUtils.isEmpty(sysUserDetails.getPost())) {
                    tv_user_type.setText(sysUserDetails.getPost());
                    tv_user_type.setVisibility(View.VISIBLE);
                } else {
                    tv_user_type.setVisibility(View.INVISIBLE);
                }
            }

        }
    }

}
